if ($args.Count -ne 1) {
	exit
}
$dir = $args[0]
$sourcefiles = "\lang\WNScriptParser.y", "\lang\WNScriptParser.y", "\lang\WNScriptParser.y", "\lang\WNQuickLexer.re", "\lang\WNScriptLexer.re"
$copyfiles = "\lang\WNScriptParser.h", "\lang\WNScriptParser.c", "\lang\WNScriptParser.out", "\gen\WNQuickLexer-temp.cpp", "\gen\WNScriptLexer-temp.cpp"
$destination = "\gen\WNScriptParser.h", "\gen\WNScriptParser.cpp", "\gen\WNScriptParser.out", "\gen\WNQuickLexer.cpp", "\gen\WNScriptLexer.cpp"

$tempfile = $env:temp + "\WNProjectBuild.txt"
$forceupdate = $FALSE
$filehasharray = @()

if (Test-Path $tempfile) {
	try {
		Get-Content $tempfile | Foreach-Object { $filehasharray += Get-Date $_ }
	} catch {
		Write-Host "Warning: Temp file seems to be in wrong format, forcing update."
		
		$forceupdate = $TRUE
	}
}
else
{
	$forceupdate = $TRUE
}

if ($filehasharray.Count -ne $copyfiles.Count){
	$forceupdate = $TRUE
	$filehasharray = @()
	
	Write-Host "Current temp file is out of date, restarting all."
}

$lastsourceFile = ""

for ($i=0; $i -lt $copyfiles.Count; $i++) {
	$file1 = $dir + $copyfiles[$i]
	$file2 = $dir + $destination[$i]
	$sourceFile = $dir + $sourcefiles[$i]
	$fs = (Get-Item $sourceFile).LastWriteTime	

	if ($forceupdate) {
		$filehasharray += $fs;
		
        if ($sourceFile -ne $lastsourceFile){
            if ($sourceFile.substring($sourceFile.length - 2, 2).CompareTo(".y") -eq 0) {
                Write-Host "Should recreate $sourceFile .... Parser"
				
				$lempar = Join-Path $dir "\lang\lempar.c"
                $cmd = 'lemon.exe T="{0}" "{1}"' -f $lempar, $sourceFile
				
				try {
					Invoke-Expression -Command $cmd -ErrorAction Stop
				} catch {
					Write-Host "Error: $error[0]"
					
					exit $lastexitcode					
				}
            } elseif($sourceFile.substring($sourceFile.length - 2, 2).CompareTo("re") -eq 0) {
                Write-Host "Should recreate $sourceFile .... Lexer"
				
                $cmd = 're2c.exe --no-generation-date -o "{0}" -b "{1}"' -f $file1, $sourceFile 
				
				try {
					Invoke-Expression -Command $cmd -ErrorAction Stop
				} catch {
					Write-Host "Error: $error[0]"
					
					exit $lastexitcode					
				}
            }
        }
		
		try {
			Copy-Item -Path $file1 -Destination $file2
			Write-Host "Copying $file1 To $file2 due to forced update"			
		} catch {
			Write-Host "Error: $error[0]"
			
			exit $lastexitcode					
		}
	} else {
		if (([string]$filehasharray[$i] -ne [string]$fs) -or ((Test-Path $file2) -eq $FALSE)) {
            if ($sourceFile.substring($sourceFile.length - 2, 2).CompareTo(".y") -eq 0){
                Write-Host "Should recreate " $sourceFile " .... Parser"
				
                $lempar = Join-Path $dir "\lang\lempar.c"
				$cmd = 'lemon.exe T="{0}" "{1}"' -f $lempar, $sourceFile
				
				try {
					Invoke-Expression -Command $cmd -ErrorAction Stop
				} catch {
					Write-Host "Error: $error[0]"
					
					exit $lastexitcode					
				}
            } elseif ($sourceFile.substring($sourceFile.length - 2, 2).CompareTo("re") -eq 0){
                Write-Host "Should recreate " $sourceFile " .... Lexer"
				
                $cmd = 're2c.exe --no-generation-date -o "{0}" -b "{1}"' -f $file1, $sourceFile
				
				try {
					Invoke-Expression -Command $cmd -ErrorAction Stop
				} catch {
					Write-Host "Error: $error[0]"
					
					exit $lastexitcode					
				}
            }
			
			$filehasharray[$i] = $fs
			
			try {
				Copy-Item -Path $file1 -Destination $file2
				Write-Host "Copying $file1 To $file2since it has changed "		
			} catch {
				Write-Host "Error: $error[0]"
				
				exit $lastexitcode					
			}
		} else {
			Write-Host "Not copying $file1 because it is up to date" 
		}
	}
	
    $lastsourceFile = $sourceFile
}

$null | Set-Content $tempfile
$filehasharray | Foreach-Object { 
	if ($_ -ne ""){
		Add-Content $tempfile $_
	}
}

exit $lastexitcode